<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Node 第 8 天 | Megasu的笔记</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="icon" href="/heima2020/favicon.ico">
    <meta name="description" content="Megasu的笔记整理">
    <meta name="author" content="Megasu">
    <meta name="Keywords" content="学习网站,前端,分享">
    <link rel="preload" href="/heima2020/assets/css/0.styles.aa663799.css" as="style"><link rel="preload" href="/heima2020/assets/js/app.e2adc9d2.js" as="script"><link rel="preload" href="/heima2020/assets/js/2.7389c9d3.js" as="script"><link rel="preload" href="/heima2020/assets/js/8.c14cc922.js" as="script"><link rel="prefetch" href="/heima2020/assets/js/10.8b750e5f.js"><link rel="prefetch" href="/heima2020/assets/js/11.9224f975.js"><link rel="prefetch" href="/heima2020/assets/js/12.3ea117b6.js"><link rel="prefetch" href="/heima2020/assets/js/13.7524b74f.js"><link rel="prefetch" href="/heima2020/assets/js/14.cee0fcf8.js"><link rel="prefetch" href="/heima2020/assets/js/15.7a4a27f7.js"><link rel="prefetch" href="/heima2020/assets/js/16.947d882b.js"><link rel="prefetch" href="/heima2020/assets/js/17.79d3becb.js"><link rel="prefetch" href="/heima2020/assets/js/18.48208e3a.js"><link rel="prefetch" href="/heima2020/assets/js/19.3d0c49ef.js"><link rel="prefetch" href="/heima2020/assets/js/20.1f6114c8.js"><link rel="prefetch" href="/heima2020/assets/js/21.85e67b4a.js"><link rel="prefetch" href="/heima2020/assets/js/22.ce482f4d.js"><link rel="prefetch" href="/heima2020/assets/js/23.a150bd35.js"><link rel="prefetch" href="/heima2020/assets/js/24.8cd80d5d.js"><link rel="prefetch" href="/heima2020/assets/js/25.0001c7c1.js"><link rel="prefetch" href="/heima2020/assets/js/26.aa4d65a5.js"><link rel="prefetch" href="/heima2020/assets/js/27.baffff1d.js"><link rel="prefetch" href="/heima2020/assets/js/28.20430dcf.js"><link rel="prefetch" href="/heima2020/assets/js/29.7bbc3c04.js"><link rel="prefetch" href="/heima2020/assets/js/3.0076a912.js"><link rel="prefetch" href="/heima2020/assets/js/30.5f74696f.js"><link rel="prefetch" href="/heima2020/assets/js/31.a0bf56d6.js"><link rel="prefetch" href="/heima2020/assets/js/32.dd5a93dd.js"><link rel="prefetch" href="/heima2020/assets/js/33.92de7335.js"><link rel="prefetch" href="/heima2020/assets/js/34.bf468ed6.js"><link rel="prefetch" href="/heima2020/assets/js/35.28502be1.js"><link rel="prefetch" href="/heima2020/assets/js/36.3622a14c.js"><link rel="prefetch" href="/heima2020/assets/js/37.626e17d3.js"><link rel="prefetch" href="/heima2020/assets/js/38.860020b8.js"><link rel="prefetch" href="/heima2020/assets/js/39.c092c283.js"><link rel="prefetch" href="/heima2020/assets/js/4.586af112.js"><link rel="prefetch" href="/heima2020/assets/js/40.a6ba2743.js"><link rel="prefetch" href="/heima2020/assets/js/41.43d70bcd.js"><link rel="prefetch" href="/heima2020/assets/js/42.6486a709.js"><link rel="prefetch" href="/heima2020/assets/js/43.670b256a.js"><link rel="prefetch" href="/heima2020/assets/js/44.eb371480.js"><link rel="prefetch" href="/heima2020/assets/js/45.5b45f11e.js"><link rel="prefetch" href="/heima2020/assets/js/46.bc59be94.js"><link rel="prefetch" href="/heima2020/assets/js/47.ec36fca9.js"><link rel="prefetch" href="/heima2020/assets/js/48.51e80f8f.js"><link rel="prefetch" href="/heima2020/assets/js/5.6eb7e243.js"><link rel="prefetch" href="/heima2020/assets/js/6.1b7d5a6d.js"><link rel="prefetch" href="/heima2020/assets/js/7.f611fe3c.js"><link rel="prefetch" href="/heima2020/assets/js/9.cd5538bb.js">
    <link rel="stylesheet" href="/heima2020/assets/css/0.styles.aa663799.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/heima2020/" class="home-link router-link-active"><!----> <span class="site-name">Megasu的笔记</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/heima2020/" class="nav-link">
  首页
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/heima2020/" class="nav-link">
  首页
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><a href="/heima2020/node/day01" class="sidebar-heading clickable open"><span>Node</span> <!----></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/heima2020/node/day01.html" class="sidebar-link">Node 第 1 天</a></li><li><a href="/heima2020/node/day02.html" class="sidebar-link">Node 第 2 天</a></li><li><a href="/heima2020/node/day03.html" class="sidebar-link">Node 第 3 天</a></li><li><a href="/heima2020/node/day04.html" class="sidebar-link">Node 第 4 天</a></li><li><a href="/heima2020/node/day05.html" class="sidebar-link">Node 第 5 天</a></li><li><a href="/heima2020/node/day06.html" class="sidebar-link">Node 第 6 天</a></li><li><a href="/heima2020/node/day07.html" class="sidebar-link">Node 第 7 天</a></li><li><a href="/heima2020/node/day08.html" aria-current="page" class="active sidebar-link">Node 第 8 天</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#node-第-8-天" class="sidebar-link">Node 第 8 天</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_1-学习目标" class="sidebar-link">1. 学习目标</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_2-新建-ev-article-cate-数据表" class="sidebar-link">2. 新建 ev_article_cate 数据表</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_3-获取文章分类列表" class="sidebar-link">3. 获取文章分类列表</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_4-新增文章分类" class="sidebar-link">4. 新增文章分类</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_5-根据-id-删除文章分类" class="sidebar-link">5. 根据 id 删除文章分类</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_6-根据-id-获取文章分类" class="sidebar-link">6. 根据 id 获取文章分类</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_7-根据-id-更新文章分类" class="sidebar-link">7. 根据 id 更新文章分类</a></li><li class="sidebar-sub-header"><a href="/heima2020/node/day08.html#_8-发布新文章" class="sidebar-link">8. 发布新文章</a></li></ul></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h2 id="node-第-8-天"><a href="#node-第-8-天" class="header-anchor">#</a> Node 第 8 天</h2> <h3 id="_1-学习目标"><a href="#_1-学习目标" class="header-anchor">#</a> 1. 学习目标</h3> <p>◆ 完成获取文章分类列表的功能</p> <p>◆ 完成新增文章分类的功能</p> <p>◆ 完成根据 ID 删除文章分类的功能</p> <p>◆ 完成根据 ID 获取文章分类的功能</p> <p>◆ 完成根据 ID 更新文章分类的功能</p> <p>◆ 完成发布新文章的功能</p> <h3 id="_2-新建-ev-article-cate-数据表"><a href="#_2-新建-ev-article-cate-数据表" class="header-anchor">#</a> 2. 新建 <code>ev_article_cate</code> 数据表</h3> <h4 id="_2-1-创建表结构"><a href="#_2-1-创建表结构" class="header-anchor">#</a> 2.1 创建表结构</h4> <img src="/heima2020/assets/img/001 - 创建文章分类的表.2eee1447.png" style="zoom:80%;"> <h4 id="_2-2-新增两条初始化的数据"><a href="#_2-2-新增两条初始化的数据" class="header-anchor">#</a> 2.2 新增两条初始化的数据</h4> <img src="/heima2020/assets/img/002 - 两条初始化的数据.66545f40.png" style="zoom:80%;"> <h3 id="_3-获取文章分类列表"><a href="#_3-获取文章分类列表" class="header-anchor">#</a> 3. 获取文章分类列表</h3> <h4 id="_3-1-实现步骤"><a href="#_3-1-实现步骤" class="header-anchor">#</a> 3.1 实现步骤</h4> <ol><li>初始化路由模块</li> <li>初始化路由处理函数模块</li> <li>获取文章分类列表数据</li></ol> <h4 id="_3-2-初始化路由模块"><a href="#_3-2-初始化路由模块" class="header-anchor">#</a> 3.2 初始化路由模块</h4> <ol><li>创建 <code>/router/artcate.js</code> 路由模块，并初始化如下的代码结构</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// // 导入 express</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;express&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 创建路由对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 获取文章分类的列表数据</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/cates&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向外共享路由对象</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> router<span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>app.js</code> 中导入并使用文章分类的路由模块</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入并使用文章分类路由模块</span>
<span class="token keyword">const</span> artCateRouter <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./router/userinfo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 为文章分类的路由挂载统一的访问前缀 /my/article</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token string">&quot;/my/article&quot;</span><span class="token punctuation">,</span> artCateRouter<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="_3-3-初始化路由处理函数模块"><a href="#_3-3-初始化路由处理函数模块" class="header-anchor">#</a> 3.3 初始化路由处理函数模块</h4> <ol><li>创建 <code>/router_handler/artcate.js</code> 路由处理函数模块，并初始化如下的代码结构</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 获取文章分类列表数据的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">getArticleCates</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>修改 <code>/router/artcate.js</code> 中的代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// // 导入 express</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;express&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 创建路由对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 导入文章分类的路由处理函数模块</span>
<span class="token keyword">const</span> artcate_handler <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../router_handler/artcate&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 获取文章分类的列表数据</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/cates&quot;</span><span class="token punctuation">,</span> artcate_handler<span class="token punctuation">.</span>getArticleCates<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向外共享路由对象</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> router<span class="token punctuation">;</span>
</code></pre></div><h4 id="_3-4-实现获取文章分类列表数据的功能"><a href="#_3-4-实现获取文章分类列表数据的功能" class="header-anchor">#</a> 3.4 实现获取文章分类列表数据的功能</h4> <ol><li>在 <code>/router_handler/artcate.js</code> 头部导入数据库操作模块</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入数据库操作模块</span>
<span class="token keyword">const</span> db <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../db/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>定义 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义查询分类列表数据的 SQL 语句</span>
<span class="token comment">// is_delete 为 0 表示没有被 标记为删除 的数据</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token string">&quot;select * from ev_article_cate where is_delete=0 order by id asc&quot;</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>调用 <code>db.query()</code> 执行 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 调用 db.query() 执行 SQL 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// SQL 语句执行失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// SQL 语句执行成功</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    status<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    message<span class="token operator">:</span> <span class="token string">&quot;获取文章分类列表成功！&quot;</span><span class="token punctuation">,</span>
    data<span class="token operator">:</span> results<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 这是路由处理函数模块</span>

<span class="token comment">// 导入数据库操作模块</span>
<span class="token keyword">const</span> db <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../db/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 获取文章分类列表数据的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">getArticleCates</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 定义查询分类列表数据的 SQL 语句</span>
  <span class="token comment">// is_delete 为 0 表示没有被 标记为删除 的数据</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token string">&quot;select * from ev_article_cate where is_delete=0 order by id asc&quot;</span><span class="token punctuation">;</span>

  <span class="token comment">// 调用 db.query() 执行 SQL 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// SQL 语句执行失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// SQL 语句执行成功</span>
    res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      status<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
      message<span class="token operator">:</span> <span class="token string">&quot;获取文章分类列表成功！&quot;</span><span class="token punctuation">,</span>
      data<span class="token operator">:</span> results<span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="5"><li>测试</li></ol> <img src="/heima2020/assets/img/003 - 实现获取文章分类列表数据的功能.92c1a2c6.png" style="zoom:80%;"> <h3 id="_4-新增文章分类"><a href="#_4-新增文章分类" class="header-anchor">#</a> 4. 新增文章分类</h3> <h4 id="_4-1-实现步骤"><a href="#_4-1-实现步骤" class="header-anchor">#</a> 4.1 实现步骤</h4> <ol><li>定义路由和处理函数</li> <li>验证表单数据</li> <li>查询 <code>分类名称</code> 与 <code>分类别名</code> 是否被占用</li> <li>实现新增文章分类的功能</li></ol> <h4 id="_4-2-定义路由和处理函数"><a href="#_4-2-定义路由和处理函数" class="header-anchor">#</a> 4.2 定义路由和处理函数</h4> <ol><li><p>在 <code>/router/artcate.js</code> 模块中，添加 <code>新增文章分类</code> 的路由</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 新增文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/addcates&quot;</span><span class="token punctuation">,</span> artcate_handler<span class="token punctuation">.</span>addArticleCates<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <ol start="2"><li>在 <code>/router_handler/artcate.js</code> 模块中，定义并向外共享 <code>新增文章分类</code> 的路由处理函数</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 新增文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticleCates</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="_4-3-验证表单数据"><a href="#_4-3-验证表单数据" class="header-anchor">#</a> 4.3 验证表单数据</h4> <ol><li>创建 <code>/schema/artcate.js</code> 文章分类数据验证模块</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 1. 导入定义验证规则的模块</span>
<span class="token keyword">const</span> joi <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;@hapi/joi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 2/ 定义 name 和 alias 的校验规则</span>
<span class="token keyword">const</span> name <span class="token operator">=</span> joi<span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> alias <span class="token operator">=</span> joi
  <span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">alphanum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向外共享验证规则对象</span>
exports<span class="token punctuation">.</span>add_cate_schema <span class="token operator">=</span> <span class="token punctuation">{</span>
  body<span class="token operator">:</span> <span class="token punctuation">{</span>
    name<span class="token punctuation">,</span>
    alias<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router/artcate.js</code> 模块中，使用 <code>add_cate_schema</code> 对数据进行验证</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入验证数据的中间件</span>
<span class="token keyword">const</span> expressJoi <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;@escook/express-joi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 导入文章分类的验证模块</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> add_cate_schema <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../schema/artcate&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 新增文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>
  <span class="token string">&quot;/addcates&quot;</span><span class="token punctuation">,</span>
  <span class="token function">expressJoi</span><span class="token punctuation">(</span>add_cate_schema<span class="token punctuation">)</span><span class="token punctuation">,</span>
  artcate_handler<span class="token punctuation">.</span>addArticleCates
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/004 - 验证表单数据.7b788f47.png"> <h4 id="_4-4-分析分类名和别名被占用的四种情况"><a href="#_4-4-分析分类名和别名被占用的四种情况" class="header-anchor">#</a> 4.4 分析分类名和别名被占用的四种情况</h4> <ol><li>分类名称和分类别名同时被占用</li></ol> <img src="/heima2020/assets/img/005 - 查询结果·1.ec12c782.png"> <ol start="2"><li>分类名称和分类别名同时被占用</li></ol> <img src="/heima2020/assets/img/006 - 查询结果 2.1bf10c31.png"> <ol start="3"><li>只有分类名称被占用</li></ol> <img src="/heima2020/assets/img/007 - 查询结果 3.6fcbf632.png"> <ol start="4"><li>只有分类别名被占用</li></ol> <img src="/heima2020/assets/img/008 - 查询结果 4.e945c2bb.png"> <h4 id="_4-5-写代码判断分类名称和别名是否被占用"><a href="#_4-5-写代码判断分类名称和别名是否被占用" class="header-anchor">#</a> 4.5 写代码判断分类名称和别名是否被占用</h4> <ol><li>定义查重的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义查询 分类名称 与 分类别名 是否被占用的 SQL 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where name=? or alias=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li><p>调用 <code>db.query()</code> 执行查重的操作</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 执行查重操作</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 执行 SQL 语句失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 分类名称 和 分类别名 都被占用</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>
    results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
    results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name <span class="token operator">&amp;&amp;</span>
    results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias
  <span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 分类名称 或 分类别名 被占用</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// TODO：新增文章分类</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 新增文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticleCates</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 定义查重的 sql 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where name=? or alias=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 2. 执行查重的 sql 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 3. SQL 语句执行失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 4.1 判断数据的 length</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 4.2 length 等于 1 的三种情况，分类名称 或 分类别名 被占用</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>
      results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
      results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name <span class="token operator">&amp;&amp;</span>
      results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias
    <span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;Ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="_4-6-实现新增文章分类的功能"><a href="#_4-6-实现新增文章分类的功能" class="header-anchor">#</a> 4.6 实现新增文章分类的功能</h4> <ol><li>定义新增文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义插入文章分类的 sql 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">insert into ev_article_cate set ?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行新增文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 执行插入文章分类的 sql 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// SQL 语句执行失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// SQL 语句执行成功，但是影响行数不等于 1</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;新增文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 新增文章分类成功</span>
  res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;新增文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 新增文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticleCates</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 定义查重的 sql 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where name=? or alias=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 2. 执行查重的 sql 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 3. SQL 语句执行失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 4.1 判断数据的 length</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 4.2 length 等于 1 的三种情况，分类名称 或 分类别名 被占用</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>
      results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
      results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name <span class="token operator">&amp;&amp;</span>
      results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias
    <span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">)</span>
      <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 定义插入文章分类的 sql 语句</span>
    <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">insert into ev_article_cate set ?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

    <span class="token comment">// 执行插入文章分类的 sql 语句</span>
    db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token comment">// SQL 语句执行失败</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// SQL 语句执行成功，但是影响行数不等于 1</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;新增文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 新增文章分类成功</span>
      res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;新增文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>测试</li></ol> <img src="/heima2020/assets/img/009 - 添加文章分类.1248ffde.png" style="zoom:80%;"> <h3 id="_5-根据-id-删除文章分类"><a href="#_5-根据-id-删除文章分类" class="header-anchor">#</a> 5. 根据 id 删除文章分类</h3> <h4 id="_5-1-实现步骤"><a href="#_5-1-实现步骤" class="header-anchor">#</a> 5.1 实现步骤</h4> <ol><li>定义路由和处理函数</li> <li>验证表单数据</li> <li>实现删除文章分类的功能</li></ol> <h4 id="_5-2-定义路由和处理函数"><a href="#_5-2-定义路由和处理函数" class="header-anchor">#</a> 5.2 定义路由和处理函数</h4> <ol><li>在 <code>/router/artcate.js</code> 模块中，添加 <code>删除文章分类</code> 的路由</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 删除文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/deletecate/:id&quot;</span><span class="token punctuation">,</span> artcate_handler<span class="token punctuation">.</span>deleteCateById<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router_handler/artcate.js</code> 模块中，定义并向外共享 <code>删除文章分类</code> 的路由处理函数</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 删除文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">deleteCateById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/010 - 定义路由和处理函数.068dc233.png" style="zoom:80%;"> <h4 id="_5-3-验证客户端提交到服务器的-id-值"><a href="#_5-3-验证客户端提交到服务器的-id-值" class="header-anchor">#</a> 5.3 验证客户端提交到服务器的 id 值</h4> <ol><li>在 <code>/schema/artcate.js</code> 验证规则模块中，定义 id 的验证规则</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义 分类 Id 的校验规则</span>
<span class="token keyword">const</span> id <span class="token operator">=</span> joi
  <span class="token punctuation">.</span><span class="token function">number</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>并使用 <code>exports</code> 向外共享如下的 <code>验证规则对象</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 向外共享删除分类的规则对象</span>
exports<span class="token punctuation">.</span>delete_cate_schema <span class="token operator">=</span> <span class="token punctuation">{</span>
  params<span class="token operator">:</span> <span class="token punctuation">{</span>
    id<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li><p>在 <code>/router/artcate.js</code> 模块中，导入需要的验证规则对象，并在路由中使用</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入删除分类的验证规则对象</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> delete_cate_schema <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../schema/artcate&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 删除文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>
  <span class="token string">&quot;/deletecate/:id&quot;</span><span class="token punctuation">,</span>
  <span class="token function">expressJoi</span><span class="token punctuation">(</span>delete_cate_schema<span class="token punctuation">)</span><span class="token punctuation">,</span>
  artcate_handler<span class="token punctuation">.</span>deleteCateById
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <ol start="4"><li>验证</li></ol> <img src="/heima2020/assets/img/011 - 验证客户端提交到服务器的 id 值.52baa676.png" style="zoom:80%;"> <h4 id="_5-4-实现删除文章分类的功能"><a href="#_5-4-实现删除文章分类的功能" class="header-anchor">#</a> 5.4 实现删除文章分类的功能</h4> <ol><li>定义删除文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 1. 定义删除的 sql 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">update ev_article_cate set is_delete=1 where id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行删除文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 2. 执行查重的 sql 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// SQL 语句执行失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 语句执行 SQL 成功，但是影响行数不等于 1</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;删除文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 删除文章分类成功</span>
  res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;删除文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 删除文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">deleteCateById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 定义删除的 sql 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">update ev_article_cate set is_delete=1 where id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 2. 执行删除的 sql 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// SQL 语句执行失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 语句执行 SQL 成功，但是影响行数不等于 1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;删除文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 删除文章分类成功</span>
    res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;删除文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>验证</li></ol> <img src="/heima2020/assets/img/012 - 实现删除文章分类的功能.85e623a0.png" style="zoom:80%;"> <h3 id="_6-根据-id-获取文章分类"><a href="#_6-根据-id-获取文章分类" class="header-anchor">#</a> 6. 根据 id 获取文章分类</h3> <h4 id="_6-1-实现步骤"><a href="#_6-1-实现步骤" class="header-anchor">#</a> 6.1 实现步骤</h4> <ol><li>定义路由和处理函数</li> <li>验证表单数据</li> <li>实现获取文章分类的功能</li></ol> <h4 id="_6-2-定义路由和处理函数"><a href="#_6-2-定义路由和处理函数" class="header-anchor">#</a> 6.2 定义路由和处理函数</h4> <ol><li>在 <code>/router/artcate.js</code> 模块中，添加 <code>根据 Id 获取文章分类</code> 的路由</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 查询文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/cates/:id&quot;</span><span class="token punctuation">,</span> artcate_handler<span class="token punctuation">.</span>getArticleById<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router_handler/artcate.js</code> 模块中，定义并向外共享 <code>根据 Id 获取文章分类</code> 的路由处理函数</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 根据 Id 获取文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">getArticleById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/013 - 定义路由和处理函数.29da9252.png" style="zoom:80%;"> <h4 id="_6-3-验证客户端提交到服务器的-id-值"><a href="#_6-3-验证客户端提交到服务器的-id-值" class="header-anchor">#</a> 6.3 验证客户端提交到服务器的 id 值</h4> <ol><li>在 <code>/schema/artcate.js</code> 验证规则模块中，使用 <code>exports</code> 向外共享如下的 <code>验证规则对象</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 向外共享根据 Id 获取分类的规则对象</span>
exports<span class="token punctuation">.</span>get_cate_schema <span class="token operator">=</span> <span class="token punctuation">{</span>
  params<span class="token operator">:</span> <span class="token punctuation">{</span>
    id<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router/artcate.js</code> 模块中，导入需要的验证规则对象，并在路由中使用</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入根据 Id 获取分类的验证规则对象</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> get_cate_schema <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../schema/artcate&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//  根据 Id 获取文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>
  <span class="token string">&quot;/cates/:id&quot;</span><span class="token punctuation">,</span>
  <span class="token function">expressJoi</span><span class="token punctuation">(</span>get_cate_schema<span class="token punctuation">)</span><span class="token punctuation">,</span>
  artcate_handler<span class="token punctuation">.</span>getArticleById
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/014 - 验证客户端提交到服务器的 id 值.79e0b538.png" style="zoom:80%;"> <h4 id="_6-4-实现获取文章分类的功能"><a href="#_6-4-实现获取文章分类的功能" class="header-anchor">#</a> 6.4 实现获取文章分类的功能</h4> <ol><li>定义根据 Id 获取文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 1. 定义根据 id 获取文章分类的 sql 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 2. 执行查询的 sql 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 执行 SQL 语句失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// SQL 语句执行成功，但是没有查询到任何数据</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;获取文章分类数据失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 把数据响应给客户端</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    status<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
    message<span class="token operator">:</span> <span class="token string">&quot;获取文章分类数据成功！&quot;</span><span class="token punctuation">,</span>
    data<span class="token operator">:</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 根据 Id 获取文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">getArticleById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 定义根据 id 获取文章分类的 sql 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 2. 执行查询的 sql 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// 执行 SQL 语句失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// SQL 语句执行成功，但是没有查询到任何数据</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;获取文章分类数据失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 把数据响应给客户端</span>
    res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
      status<span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
      message<span class="token operator">:</span> <span class="token string">&quot;获取文章分类数据成功！&quot;</span><span class="token punctuation">,</span>
      data<span class="token operator">:</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>验证</li></ol> <img src="/heima2020/assets/img/015 - 实现获取文章分类的功能.e3aaffdc.png" style="zoom:80%;"> <h3 id="_7-根据-id-更新文章分类"><a href="#_7-根据-id-更新文章分类" class="header-anchor">#</a> 7. 根据 id 更新文章分类</h3> <h4 id="_7-1-实现步骤"><a href="#_7-1-实现步骤" class="header-anchor">#</a> 7.1 实现步骤</h4> <ol><li>定义路由和处理函数</li> <li>验证表单数据</li> <li>查询 <code>分类名称</code> 与 <code>分类别名</code> 是否被占用</li> <li>实现更新文章分类的功能</li></ol> <h4 id="_7-2-定义路由和处理函数"><a href="#_7-2-定义路由和处理函数" class="header-anchor">#</a> 7.2 定义路由和处理函数</h4> <ol><li>在 <code>/router/artcate.js</code> 模块中，添加 <code>更新文章分类</code> 的路由</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 更新文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/updatecate&quot;</span><span class="token punctuation">,</span> artcate_handler<span class="token punctuation">.</span>updateCateById<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router_handler/artcate.js</code> 模块中，定义并向外共享 <code>更新文章分类</code> 的路由处理函数</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 更新文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">updateCateById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/06 - 定义路由和处理函数.757547a0.png" style="zoom:80%;"> <h4 id="_7-3-验证表单数据"><a href="#_7-3-验证表单数据" class="header-anchor">#</a> 7.3 验证表单数据</h4> <ol><li>在 <code>/schema/artcate.js</code> 验证规则模块中，使用 <code>exports</code> 向外共享如下的 <code>验证规则对象</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 向外共享更新分类的规则对象</span>
exports<span class="token punctuation">.</span>update_cate_schema <span class="token operator">=</span> <span class="token punctuation">{</span>
  body<span class="token operator">:</span> <span class="token punctuation">{</span>
    Id<span class="token operator">:</span> id<span class="token punctuation">,</span>
    name<span class="token punctuation">,</span>
    alias<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router/artcate.js</code> 模块中，导入需要的验证规则对象，并在路由中使用</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入更新文章分类的验证规则对象</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> update_cate_schema <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../schema/artcate&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 更新文章分类的路由</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>
  <span class="token string">&quot;/updatecate&quot;</span><span class="token punctuation">,</span>
  <span class="token function">expressJoi</span><span class="token punctuation">(</span>update_cate_schema<span class="token punctuation">)</span><span class="token punctuation">,</span>
  artcate_handler<span class="token punctuation">.</span>updateCateById
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/016 - 验证表单数据.edd2f618.png" style="zoom:80%;"> <h4 id="_7-4-查询分类名称与别名是否被占用"><a href="#_7-4-查询分类名称与别名是否被占用" class="header-anchor">#</a> 7.4 查询分类名称与别名是否被占用</h4> <ol><li>定义查重的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 1. 定义查询 分类名称 与 分类别名 是否被占用的 SQL 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where Id&lt;&gt;? and (name=? or alias=?)</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行查重的操作</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 2. 执行查重的 sql 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>Id<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 执行 SQL 语句失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 分类名称 和 分类别名 都被占用</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>
    results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
    results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name <span class="token operator">&amp;&amp;</span>
    results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias
  <span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 分类名称 或 分类别名 被占用</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/016 - 验证表单数据.edd2f618.png" style="zoom:80%;"> <h4 id="_7-5-实现更新文章分类的功能"><a href="#_7-5-实现更新文章分类的功能" class="header-anchor">#</a> 7.5 实现更新文章分类的功能</h4> <ol><li>定义更新文章分类的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 更新分类的 sql 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">update ev_article_cate set ? where Id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 执行 sql 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>Id<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// SQL 语句执行失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// SQL 语句执行成功，但是影响行数不等于 1</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;更新文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 更新文章分类成功</span>
  res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;更新文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 更新文章分类的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">updateCateById</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 1. 定义查询 分类名称 与 分类别名 是否被占用的 SQL 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">select * from ev_article_cate where Id&lt;&gt;? and (name=? or alias=?)</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 2. 执行查重的 sql 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>
    sql<span class="token punctuation">,</span>
    <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>Id<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token comment">// 执行 SQL 语句失败</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 分类名称 和 分类别名 都被占用</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>
        results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span>
        results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name <span class="token operator">&amp;&amp;</span>
        results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias
      <span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称与别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// 分类名称 或 分类别名 被占用</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类名称被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">1</span> <span class="token operator">&amp;&amp;</span> results<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>alias <span class="token operator">===</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>alias<span class="token punctuation">)</span>
        <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;分类别名被占用，请更换后重试！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      <span class="token comment">// 更新分类的 sql 语句</span>
      <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">update ev_article_cate set ? where Id=?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

      <span class="token comment">// 执行 sql 语句</span>
      db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token punctuation">[</span>req<span class="token punctuation">.</span>body<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">.</span>Id<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token comment">// SQL 语句执行失败</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// SQL 语句执行成功，但是影响行数不等于 1</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;更新文章分类失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">// 更新文章分类成功</span>
        res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;更新文章分类成功！&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>验证</li></ol> <img src="/heima2020/assets/img/017 - 实现更新文章分类的功能.615fe320.png" style="zoom:80%;"> <h3 id="_8-发布新文章"><a href="#_8-发布新文章" class="header-anchor">#</a> 8. 发布新文章</h3> <h4 id="_8-1-新建-ev-articles-表"><a href="#_8-1-新建-ev-articles-表" class="header-anchor">#</a> 8.1 新建 <code>ev_articles</code> 表</h4> <img src="/heima2020/assets/img/018 - 新建 ev_articles 表.8d751e8c.png" style="zoom:80%;"> <h4 id="_8-2-实现步骤"><a href="#_8-2-实现步骤" class="header-anchor">#</a> 8.2 实现步骤</h4> <ol><li>初始化路由模块</li> <li>初始化路由处理函数模块</li> <li>使用 <code>multer</code> 解析表单数据</li> <li>验证表单数据</li> <li>实现发布文章的功能</li></ol> <h4 id="_8-3-初始化路由模块"><a href="#_8-3-初始化路由模块" class="header-anchor">#</a> 8.3 初始化路由模块</h4> <ol><li>创建 <code>/router/article.js</code> 路由模块，并初始化如下的代码结构</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入 express</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;express&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建路由对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/add&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向外共享路由对象</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> router<span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>app.js</code> 中导入并使用文章的路由模块</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入并使用文章路由模块</span>
<span class="token keyword">const</span> articleRouter <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./router/article&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 为文章的路由挂载统一的访问前缀 /my/article</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token string">&quot;/my/article&quot;</span><span class="token punctuation">,</span> articleRouter<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="_8-4-初始化路由处理函数模块"><a href="#_8-4-初始化路由处理函数模块" class="header-anchor">#</a> 8.4 初始化路由处理函数模块</h4> <ol><li>创建 <code>/router_handler/article.js</code> 路由处理函数模块，并初始化代码结构</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 发布新文章的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li><p>修改 <code>/router/article.js</code> 中的代码</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入文章的路由处理函数模块</span>
<span class="token keyword">const</span> article_handler <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../router_handler/article&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/add&quot;</span><span class="token punctuation">,</span> article_handler<span class="token punctuation">.</span>addArticle<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入 express</span>
<span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;express&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建路由对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 导入文章的路由处理函数模块</span>
<span class="token keyword">const</span> article_handler <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../router_handler/article&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/add&quot;</span><span class="token punctuation">,</span> article_handler<span class="token punctuation">.</span>addArticle<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 向外共享路由对象</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> router<span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li><p>验证</p> <img src="/heima2020/assets/img/018 - 初始化路由处理函数模块.23bed74b.png" style="zoom:80%;"></li></ol> <h4 id="_8-5-了解-multer-的作用"><a href="#_8-5-了解-multer-的作用" class="header-anchor">#</a> 8.5 了解 <code>multer</code> 的作用</h4> <ol><li>使用 <code>express.urlencoded()</code> 中间件无法解析 <code>multipart/form-data</code> 格式的请求体数据，也就是说无法接收到 <code>fomData</code> 表单传递过来的数据</li> <li>推荐使用 <code>multer</code> 来解析 <code>multipart/form-data</code> 格式的表单数据</li></ol> <h4 id="_8-6-安装和配置-multer"><a href="#_8-6-安装和配置-multer" class="header-anchor">#</a> 8.6 安装和配置 <code>multer</code></h4> <ol><li><p>运行如下的终端命令，在项目中安装 <code>multer</code></p> <div class="language-js extra-class"><pre class="language-js"><code>cnpm i multer@<span class="token number">1.4</span><span class="token number">.2</span> <span class="token operator">-</span><span class="token constant">S</span>
</code></pre></div></li></ol> <ol start="2"><li>在 <code>/router/article.js</code> 模块中导入并配置 <code>multer</code></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入解析 formdata 格式表单数据的包</span>
<span class="token keyword">const</span> multer <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;multer&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 导入处理路径的核心模块</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;path&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 创建 multer 的实例对象，通过 dest 属性指定文件的存放路径</span>
<span class="token keyword">const</span> upload <span class="token operator">=</span> <span class="token function">multer</span><span class="token punctuation">(</span><span class="token punctuation">{</span> dest<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">&quot;../uploads&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="_8-7-使用-multer解析-formdata-格式的表单数据"><a href="#_8-7-使用-multer解析-formdata-格式的表单数据" class="header-anchor">#</a> 8.7 使用 <code>multer</code>解析 <code>FormData</code> 格式的表单数据</h4> <ol><li>修改 <code>发布新文章</code> 的路由</li></ol> <ul><li>使用 <code>upload.single()</code> 方法，它是一个局部生效的中间件，用来解析 <code>FormData</code> 格式的表单数据</li> <li>将文件类型的数据，解析并挂载到 <code>req.file</code> 属性中</li> <li>将文本类型的数据，解析并挂载到 <code>req.body</code> 属性中</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 发布新文章的路由</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/add&quot;</span><span class="token punctuation">,</span> upload<span class="token punctuation">.</span><span class="token function">single</span><span class="token punctuation">(</span><span class="token string">&quot;cover_img&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span> article_handler<span class="token punctuation">.</span>addArticle<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>在 <code>/router_handler/article.js</code> 模块的 <code>addArticle</code> 处理函数中，将 <code>multer</code> 解析出来的数据进行打印</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 发布新文章的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文本类型的数据</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;--------分割线----------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文件类型的数据</span>

  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/019 - 解析表单数据.38a0ff85.png" style="zoom:80%;"> <h4 id="_8-8-对-req-body-中的数据进行验证"><a href="#_8-8-对-req-body-中的数据进行验证" class="header-anchor">#</a> 8.8 对 <code>req.body</code> 中的数据进行验证</h4> <ol><li>实现思路：通过 <code>express-joi</code><strong>自动验证</strong> <code>req.body</code> 中的文本数据；通过 <code>if</code> 判断<strong>手动验证</strong> <code>req.file</code> 中的文件数据</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入定义验证规则的模块</span>
<span class="token keyword">const</span> joi <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;@hapi/joi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义 标题、分类Id、内容、发布状态 的验证规则</span>
<span class="token keyword">const</span> title <span class="token operator">=</span> joi<span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> cate_id <span class="token operator">=</span> joi
  <span class="token punctuation">.</span><span class="token function">number</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> content <span class="token operator">=</span> joi
  <span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">allow</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> state <span class="token operator">=</span> joi
  <span class="token punctuation">.</span><span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">valid</span><span class="token punctuation">(</span><span class="token string">&quot;已发布&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;草稿&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">required</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// // 向外共享发布文章的规则对象</span>
exports<span class="token punctuation">.</span>add_article_schema <span class="token operator">=</span> <span class="token punctuation">{</span>
  body<span class="token operator">:</span> <span class="token punctuation">{</span>
    title<span class="token punctuation">,</span>
    cate_id<span class="token punctuation">,</span>
    content<span class="token punctuation">,</span>
    state<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li><p>在 <code>/router/article.js</code> 模块中，导入需要的验证规则对象，并在路由中使用</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入验证数据的中间件</span>
<span class="token keyword">const</span> expressJoi <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;@escook/express-joi&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 导入文章的验证模块</span>
<span class="token keyword">const</span> <span class="token punctuation">{</span> add_article_schema <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../schema/article&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章</span>
<span class="token comment">// 注意：在当前的路由中，先后使用了两个中间件：</span>
<span class="token comment">//  先使用 multer 解析表单数据</span>
<span class="token comment">//  再使用 expressJoi 对解析的表单数据进行验证</span>
router<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>
  <span class="token string">&quot;/add&quot;</span><span class="token punctuation">,</span>
  upload<span class="token punctuation">.</span><span class="token function">single</span><span class="token punctuation">(</span><span class="token string">&quot;cover_img&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token function">expressJoi</span><span class="token punctuation">(</span>add_article_schema<span class="token punctuation">)</span><span class="token punctuation">,</span>
  article_handler<span class="token punctuation">.</span>addArticle
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <h4 id="_8-9-对-req-file-中的数据进行验证"><a href="#_8-9-对-req-file-中的数据进行验证" class="header-anchor">#</a> 8.9 对 <code>req.file</code> 中的数据进行验证</h4> <ol><li><p>在 <code>/router_handler/article.js</code> 模块中的 <code>addArticle</code> 处理函数中，通过 <code>if</code> 判断客户端是否提交了 <code>封面图片</code></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 发布新文章的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文本类型的数据</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;--------分割线----------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文件类型的数据</span>

  <span class="token comment">// 判断客户端是否提交了 封面图片</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>req<span class="token punctuation">.</span>file <span class="token operator">||</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>fieldname <span class="token operator">!==</span> <span class="token string">&quot;cover_img&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;文章封面是必选参数！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;ok&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div></li></ol> <h4 id="_8-10-处理文章信息对象"><a href="#_8-10-处理文章信息对象" class="header-anchor">#</a> 8.10 处理文章信息对象</h4> <ol><li>整理要插入数据库的文章信息对象</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> articleInfo <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token comment">// 标题、内容、状态、所属的分类Id</span>
  <span class="token operator">...</span>req<span class="token punctuation">.</span>body<span class="token punctuation">,</span>
  <span class="token comment">// 文章封面在服务器端的存放路径</span>
  cover_img<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;/uploads&quot;</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>filename<span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token comment">// 文章发布时间</span>
  pub_date<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token comment">// 文章作者的Id</span>
  author_id<span class="token operator">:</span> req<span class="token punctuation">.</span>user<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入处理路径的 path 核心模块</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;path&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文本类型的数据</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;--------分割线----------&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 文件类型的数据</span>

  <span class="token comment">// 判断客户端是否提交了 封面图片</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>req<span class="token punctuation">.</span>file <span class="token operator">||</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>fieldname <span class="token operator">!==</span> <span class="token string">&quot;cover_img&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;文章封面是必选参数！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">const</span> articleInfo <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// 标题、内容、状态、所属的分类Id</span>
    <span class="token operator">...</span>req<span class="token punctuation">.</span>body<span class="token punctuation">,</span>
    <span class="token comment">// 文章封面在服务器端的存放路径</span>
    cover_img<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;/uploads&quot;</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>filename<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// 文章发布时间</span>
    pub_date<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// 文章作者的Id</span>
    author_id<span class="token operator">:</span> req<span class="token punctuation">.</span>user<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>articleInfo<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>验证</li></ol> <img src="/heima2020/assets/img/021 - 处理文章信息对象.a117577c.png" style="zoom:80%;"> <h4 id="_8-11-实现发布文章的功能"><a href="#_8-11-实现发布文章的功能" class="header-anchor">#</a> 8.11 实现发布文章的功能</h4> <ol><li>定义发布文章的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义发布文章的 sql 语句</span>
<span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">insert into ev_articles set ?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
</code></pre></div><ol start="2"><li>调用 <code>db.query()</code> 执行发布文章的 <code>SQL</code> 语句</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入数据库操作模块</span>
<span class="token keyword">const</span> db <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../db/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 执行 SQL 语句</span>
db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> articleInfo<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 执行 SQL 语句失败</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 执行 SQL 语句成功，但是影响行数不等于 1</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;发布文章失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 发布文章成功</span>
  res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;发布文章成功&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>完整代码</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 导入处理路径的 path 核心模块</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;path&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 发布新文章的处理函数</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">addArticle</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 判断客户端是否提交了 封面图片</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>req<span class="token punctuation">.</span>file <span class="token operator">||</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>fieldname <span class="token operator">!==</span> <span class="token string">&quot;cover_img&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;文章封面是必选参数！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">const</span> articleInfo <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// 标题、内容、状态、所属的分类Id</span>
    <span class="token operator">...</span>req<span class="token punctuation">.</span>body<span class="token punctuation">,</span>
    <span class="token comment">// 文章封面在服务器端的存放路径</span>
    cover_img<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;/uploads&quot;</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span>file<span class="token punctuation">.</span>filename<span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// 文章发布时间</span>
    pub_date<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token comment">// 文章作者的Id</span>
    author_id<span class="token operator">:</span> req<span class="token punctuation">.</span>user<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token comment">// 定义发布文章的 sql 语句</span>
  <span class="token keyword">const</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">insert into ev_articles set ?</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>

  <span class="token comment">// 导入数据库操作模块</span>
  <span class="token keyword">const</span> db <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../db/index&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// 执行 SQL 语句</span>
  db<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> articleInfo<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err<span class="token punctuation">,</span> results</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">// SQL 执行语句失败</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 执行 SQL 语句成功，但是影响行数不等于 1</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>results<span class="token punctuation">.</span>affectedRows <span class="token operator">!==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">return</span> res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;发布文章失败！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 发布文章成功</span>
    res<span class="token punctuation">.</span><span class="token function">cc</span><span class="token punctuation">(</span><span class="token string">&quot;发布文章成功&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><ol start="4"><li>验证</li></ol> <img src="/heima2020/assets/img/022 - 实现发布文章的功能.2a9974d6.png" style="zoom:80%;"> <h4 id="_8-12-托管静态资源文件"><a href="#_8-12-托管静态资源文件" class="header-anchor">#</a> 8.12 托管静态资源文件</h4> <ol><li>在 <code>app.js</code> 中，使用 <code>express.static()</code> 中间件，将 <code>uploads</code> 目录中的图片托管为静态资源</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 托管静态资源文件</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token string">&quot;/uploads&quot;</span><span class="token punctuation">,</span> express<span class="token punctuation">.</span><span class="token function">static</span><span class="token punctuation">(</span><span class="token string">&quot;./uploads&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/heima2020/node/day07.html" class="prev">
        Node 第 7 天
      </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/heima2020/assets/js/app.e2adc9d2.js" defer></script><script src="/heima2020/assets/js/2.7389c9d3.js" defer></script><script src="/heima2020/assets/js/8.c14cc922.js" defer></script>
  </body>
</html>
